package com.awesome.okhttpdemo.okhttp1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.awesome.okhttpdemo.R;
import com.awesome.okhttpdemo.okhttp1.interceptor.LoggingInterceptor;
import com.awesome.okhttpdemo.okhttp1.request.CommonRequest;
import com.awesome.okhttpdemo.okhttp1.listener.DisposeDataListener;
import com.awesome.okhttpdemo.okhttp1.util.RequestCenter;
import com.awesome.sdk.util.ShowLogUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Response;

/**
 * get类型网址：http://www.wanandroid.com/     https://kyfw.12306.cn/otn/
 */
public class Okhttp1Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 封装Request
        OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new LoggingInterceptor())
                .build();
        final Call call = mOkHttpClient.newCall(CommonRequest.createGetRequest("http://www.wanandroid.com/", null));
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response response = call.execute();
                    String string = response.body().string();
                    ShowLogUtil.info(string);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        // 封装回调类
//                new DisposeDataListener() {
//                    @Override
//                    public void onSuccess(Object responseObj) {
//                        ShowLogUtil.info("onSuccess");
//                        ShowLogUtil.info(responseObj.toString());
//                    }
//
//                    @Override
//                    public void onFailure(Object reasonObj) {
//                        ShowLogUtil.info("onFailure");
//                        ShowLogUtil.info(reasonObj.toString());
//                    }
//                }, null);

        /**
         * OkHttp封装
         * step1-1:调用登录方法。传递真实的参数信息，实现回调方法
         */
        /**
         * OkHttp封装
         * step2-6:调用登录方法。
         */
        RequestCenter.getList("4", "10", new DisposeDataListener() {
            @Override
            public void onSuccess(Object responseObj) {
                ShowLogUtil.info("onSuccess");
                ShowLogUtil.info(responseObj.toString());
            }

            @Override
            public void onFailure(Object reasonObj) {
                ShowLogUtil.info("onFailure");
                ShowLogUtil.info(reasonObj.toString());
            }
        });
    }
}